package LCR;

public class minSubArrayLen {

    public int minSubArrayLen(int target, int[] nums) {
        //滑动窗口
        int ans = Integer.MAX_VALUE;
        int start = 0;
        int end = 0;
        int sum = 0;
        while(end <  nums.length) {
            sum += nums[end];
            while(sum >= target) {
                int len = end - start +1;
                if(ans > len) {
                    ans = len;
                }
                sum -= nums[start];
                start++;
            }
            end++;
        }
        if(ans == Integer.MAX_VALUE) {
            ans = 0;
        }
        return ans;
    }


    public int minSubArrayLen2(int target, int[] nums) {
        int ret = nums.length*2;
        for(int i = 0; i < nums.length;i++) {
            int tem = 0;
            int sum = 0;
            for(int j = i;j < nums.length;j++) {
                sum += nums[j];
                tem++;
                if(sum >= target) {
                    break;
                }

            }
            if(sum >= target) {
                if(ret > tem) {
                    ret = tem;
                }
            }


        }
        if(ret == nums.length*2){
            ret = 0;
        }
        return ret;

    }
}
